package com.example.datastoredemo

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.*
import androidx.datastore.preferences.createDataStore
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

/**
 * 作者: CQ
 * 日期: 2021-03-31
 * 说明: dataStore-preferences使用
 */
class DataStoreUtils {

    lateinit var context: Context
    private val preferenceName = "settings"
    var dataStore: DataStore<Preferences>

    constructor(context: Context) {
        this.context = context
    }

    init {
        dataStore = context.createDataStore(
            name = preferenceName
        )
    }

    fun putValue(key: String, value: Int) {
        GlobalScope.launch {
            val keys = intPreferencesKey(key)
            //3.存储数据
            dataStore.edit {
                it[keys] = value
            }
        }
    }

    fun putValue(key: String, value: String) {
        GlobalScope.launch {
            val keys = stringPreferencesKey(key)
            //3.存储数据
            dataStore.edit {
                it[keys] = value
            }
        }
    }
    fun putValue(key: String, value: Double) {
        GlobalScope.launch {
            val keys = doublePreferencesKey(key)
            //3.存储数据
            dataStore.edit {
                it[keys] = value
            }
        }
    }

    fun putValue(key: String, value: Boolean) {
        GlobalScope.launch {
            val keys = booleanPreferencesKey(key)
            //3.存储数据
            dataStore.edit {
                it[keys] = value
            }
        }
    }

    fun putValue(key: String, value: Float) {
        GlobalScope.launch {
            val keys = floatPreferencesKey(key)
            //3.存储数据
            dataStore.edit {
                it[keys] = value
            }
        }
    }

    fun putValue(key: String, value: Any) {
        GlobalScope.launch {
            when (value) {
                is String -> {
                    val keys = stringPreferencesKey(key)
                    //3.存储数据
                    dataStore.edit {
                        it[keys] = value
                    }
                }
                is Int -> {
                    val keys = intPreferencesKey(key)
                    dataStore.edit {
                        it[keys] = value
                    }
                }
                is Double -> {
                    val keys = doublePreferencesKey(key)
                    dataStore.edit {
                        it[keys] = value
                    }
                }
                is Boolean -> {
                    val keys = booleanPreferencesKey(key)
                    dataStore.edit {
                        it[keys] = value
                    }
                }
                is Float -> {
                    val keys = floatPreferencesKey(key)
                    dataStore.edit {
                        it[keys] = value
                    }
                }
                is Long -> {
                    val keys = longPreferencesKey(key)
                    dataStore.edit {
                        it[keys] = value
                    }
                }
            }
        }
    }

    inline fun <reified T : Any> getValue(key: String): Any =
        GlobalScope.launch {
            when (T::class) {
                Int::class -> {
                    val keys = intPreferencesKey(key)
                    dataStore.data.map {
                        it[keys]
                    }
                }
                String::class -> {
                    val keys = stringPreferencesKey(key)
                    dataStore.data.map {
                        it[keys]
                    }
                }
                Double::class -> {
                    val keys = doublePreferencesKey(key)
                    dataStore.data.map {
                        it[keys]
                    }
                }
                Float::class -> {
                    val keys = floatPreferencesKey(key)
                    dataStore.data.map {
                        it[keys]
                    }
                }
                Boolean::class -> {
                    val keys = booleanPreferencesKey(key)
                    dataStore.data.map {
                        it[keys]
                    }
                }
            }
        }

    fun getValueInt(key: String) = GlobalScope.launch {
        val keys = intPreferencesKey(key)
        dataStore.data.map {
            it[keys]
        }
    }

    fun getValueString(key: String) = GlobalScope.launch {
        val keys = stringPreferencesKey(key)
        dataStore.data.map {
            it[keys]
        }
    }

    fun getValueBoolean(key: String) = GlobalScope.launch {
        val keys = booleanPreferencesKey(key)
        dataStore.data.map {
            it[keys]
        }
    }

    fun getValueDouble(key: String) = GlobalScope.launch {
        val keys = doublePreferencesKey(key)
        dataStore.data.map {
            it[keys]
        }
    }

    fun getValueFloat(key: String) = GlobalScope.launch {
        val keys = floatPreferencesKey(key)
        dataStore.data.map {
            it[keys]
        }
    }

}